package com.wanmait.recruit.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.pojo.Company;
import com.wanmait.recruit.pojo.CompanyAdmin;
import com.wanmait.recruit.pojo.CompanyHr;
import com.wanmait.recruit.pojo.Person;
import com.wanmait.recruit.service.CompanyAdminService;
import com.wanmait.recruit.service.CompanyHrService;
import com.wanmait.recruit.service.CompanyPositionService;
import com.wanmait.recruit.service.PersonService;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@Controller
@RequestMapping("/web/company/manage/hr")
public class CompanyHrController {

    @Resource
    CompanyAdminService companyAdminService;

    @Resource
    PersonService personService;

    @Resource
    CompanyHrService companyHrService;

    @Resource
    CompanyPositionService companyPositionService;

    @RequestMapping("/hrSearch")
    public ModelAndView hrSearch(String email){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/company/manage/hr/hrAdd");
        Person person = personService.checkEmail(email);
        if(person==null){
            mv.addObject("result","ntexist");
            return mv;
        }
        CompanyHr companyHr = companyHrService.findByPerson(person);
        if(companyHr!=null){
            mv.addObject("result","exist");
            return mv;
        }
        mv.addObject("hr",person);
        return mv;
    }

    @PostMapping("/hrAdd")
    public void hrAdd(Integer id, HttpSession session, HttpServletResponse response, HttpServletRequest request) throws IOException {
        Person person = (Person)session.getAttribute("person");
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        Company company = companyAdmin.getCompany();
        CompanyHr companyHr = new CompanyHr();
        companyHr.setCompanyId(company.getId());
        companyHr.setPersonId(id);
        companyHrService.insert(companyHr);
        PrintWriter pw = response.getWriter();
        pw.print("success");
    }

    @GetMapping("/hrAdd")
    public void hrAdd(){}

    @RequestMapping("/deleteHr")
    public void deleteHr(Integer id){

        companyHrService.updateHrByPersonId(id);
        //删除该HR发布的岗位信息
        companyPositionService.deleteByPersonId(id);
    }

    @RequestMapping("/hrListData")
    public ModelAndView hrListData(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        Company company = companyAdmin.getCompany();
        PageHelper.startPage(PageContext.getPageNum(),2);
        List<Person> persons = companyHrService.findByCompany(company);
        persons.forEach(person1 -> person1.setIsCompanyHr(true));
        PageInfo<Person> pageInfo = new PageInfo<>(persons);
        ModelAndView mv = new ModelAndView();
        mv.addObject("persons",persons);
        mv.addObject(pageInfo);
        mv.setViewName("web/company/manage/hr/hrListData");
        return mv;
    }

    @RequestMapping("/hrList")
    public ModelAndView hrList(HttpSession session){
        Person person = (Person)session.getAttribute("person");
        CompanyAdmin companyAdmin = companyAdminService.findByPerson(person);
        Company company = companyAdmin.getCompany();
        PageHelper.startPage(PageContext.getPageNum(),2);
        List<Person> persons = companyHrService.findByCompany(company);
        persons.forEach(person1 -> person1.setIsCompanyHr(true));
        PageInfo<Person> pageInfo = new PageInfo<>(persons);
        ModelAndView mv = new ModelAndView();
        mv.addObject("persons",persons);
        mv.addObject(pageInfo);
        mv.setViewName("web/company/manage/hr/hrList");
        return mv;
    }
}
